package activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import net.ck.hzq.R;

import org.greenrobot.eventbus.EventBus;

import Utils.AccountUtils;
import Utils.HelloWordModel;
import Utils.ToastUtil;
import adapter.WorkOrderListAdapter;
import base.BaseActivity;
import dao.BaseOutput;
import dao.CustomIdInput;
import dao.WorkOrderListOutput;
import event.LoginEvent;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WorkOrderDoneActivity extends BaseActivity {

    private WorkOrderListAdapter mAdapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, WorkOrderDoneActivity.class);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_work_order_done;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        findViewById(R.id.iv_back).setOnClickListener(v -> finish());

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new WorkOrderListAdapter(this);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getWorkOrderList();
    }

    private void getWorkOrderList() {
        showProgressDialog();
        CustomIdInput input = new CustomIdInput();
        input.customId = AccountUtils.getCustomId();
        HelloWordModel.getInstance(this).getWorkOrderListDone(input).enqueue(new Callback<BaseOutput<WorkOrderListOutput>>() {
            @Override
            public void onResponse(Call<BaseOutput<WorkOrderListOutput>> call,
                                   Response<BaseOutput<WorkOrderListOutput>> response) {
                dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                try {
                    if (response.body() != null && "401".equals(response.body().result)) {
                        EventBus.getDefault().post(new LoginEvent());
                    } else if (response.body().msg.equals("success") && response.body().data != null) {
                        mAdapter.setupData(response.body().data.items);
                    } else {
                        ToastUtil.showNormal(WorkOrderDoneActivity.this, response.body().msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<BaseOutput<WorkOrderListOutput>> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

}
